/**
 * 
 */
package com.pms.framework;

import java.util.Hashtable;

import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

/**
 * @author yagnesh
 *
 */
public class PageDecoratorXmlLoader extends XMLParser {

	private static PageDecoratorXmlLoader pageDecoratorXmlLoader = null;
	private Hashtable pageDecoratorHashtable = new Hashtable();
	private String type;
	PageDecorator pageDecorator;
	private String headerPath;
	private String navigationPath;
	private String footerPath;
	
	
	private PageDecoratorXmlLoader() {
	}
	
	public static PageDecoratorXmlLoader getInstance() {
		if (pageDecoratorXmlLoader == null) {
			pageDecoratorXmlLoader = new PageDecoratorXmlLoader();
		}
		return pageDecoratorXmlLoader;
	}
	
	/**
	 * The method loads all the ActionElements, FormBeans and LandingPages from
	 * the XML File
	 *
	 * @param aResourceFile The resourcefile which contains all the information
	 *   of the Controller Engine in an XML format
	 */

	public Hashtable loadSettings(String aResourceFile) {
		//Load the settings from the resource file
		setXMLFile(aResourceFile);
		setHandler(this);
		try {
			processFile();
		} catch (Exception e) {
			System.out.println("\n\n>>>>>>>>>Error occured - ");
			e.printStackTrace();
		}
		return pageDecoratorHashtable;
	}



	/**
	 * @param mXMLFile
	 * @param mHandler
	 */
	public PageDecoratorXmlLoader(String mXMLFile, DefaultHandler mHandler) {
		super(mXMLFile, mHandler);
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see com.travis.ebus.frontcontroller.XMLParser#handleString(java.lang.String)
	 */
	void handleString(String mData) {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see com.travis.ebus.frontcontroller.XMLParser#handleStartDocument()
	 */
	void handleStartDocument() {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see com.travis.ebus.frontcontroller.XMLParser#handleEndDocument()
	 */
	void handleEndDocument() {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see com.travis.ebus.frontcontroller.XMLParser#handleStartElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
	 */
	void handleStartElement(String mNamespaceURI, String mSName, String mQName,Attributes mAttrs) {
		if (mQName.equalsIgnoreCase("USER")) {
			//System.out.println("Page Element Starts...");
			pageDecorator = new PageDecorator();
			if((mAttrs.getQName(0)).equalsIgnoreCase("type"))
			{
				
				type = mAttrs.getValue(0);
				pageDecorator.setType(type);
			}
		}
		if (mQName.equalsIgnoreCase("header")) {
			//System.out.println("Page Element Starts...");
			if((mAttrs.getQName(0)).equalsIgnoreCase("path"))
			{	
				headerPath = mAttrs.getValue(0);
				pageDecorator.setHeaderJsp(headerPath);
			}
		}
		if (mQName.equalsIgnoreCase("navigation")) {
			//System.out.println("Page Element Starts...");
			if((mAttrs.getQName(0)).equalsIgnoreCase("path"))
			{	
				navigationPath = mAttrs.getValue(0);
				pageDecorator.setNavigationJsp(navigationPath);
			}
		}
		if (mQName.equalsIgnoreCase("footer")) {
			//System.out.println("Page Element Starts...");
			if((mAttrs.getQName(0)).equalsIgnoreCase("path"))
			{	
				footerPath = mAttrs.getValue(0);
				pageDecorator.setFooterJsp(footerPath);
			}
		}

	}
	
	

	/* (non-Javadoc)
	 * @see com.travis.ebus.frontcontroller.XMLParser#handleEndElement(java.lang.String, java.lang.String, java.lang.String)
	 */
	void handleEndElement(String mNamespaceURI, String mSName, String mQName) {
		// TODO Auto-generated method stub
		if(mQName.equalsIgnoreCase("user")) {
			pageDecoratorHashtable.put(type,pageDecorator);			
		}
		if(mQName.equalsIgnoreCase("header")) {
			pageDecoratorHashtable.put(headerPath,pageDecorator);			
		}
		if(mQName.equalsIgnoreCase("navigation")) {
			pageDecoratorHashtable.put(navigationPath,pageDecorator);			
		}
		if(mQName.equalsIgnoreCase("footer")) {
			pageDecoratorHashtable.put(footerPath,pageDecorator);			
		}
	}

}
